package cn.uniquetech.v1;

import java.util.List;

import cn.uniquetech.util.FileUtil;
import cn.uniquetech.util.StrUtil;

public class ProUtil {
	
	private static String xmlStr = "";
	private static String headerStr = "\t";
	private static String tailStr = "\r\n";
	private static String entity = "";
	
	/**
	 * 
	 * @param packageName 包名
	 * @param tableName 表名
	 * @param colNames 字段集合
	 * @return
	 */
	public static String paraseSql( String packageName,String tableName, List<String> colNames){
		
		String servicePackage = packageName + ".entity.sqlmap";
		String capName = StrUtil.initCap(tableName);
		capName = capName.substring(0, capName.length() - 1);
		String entityName = packageName + ".entity." + capName + entity ;//+ "Entity";
		xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + tailStr;
		xmlStr += "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">" + tailStr;
		xmlStr += "<mapper namespace=\"" + capName + "Mapper\">" + tailStr;
		
		// addEntity
		xmlStr += headerStr + "<!-- 插入实体 -->" + tailStr;
		xmlStr += headerStr + "<insert id=\"addEntity\" parameterType=\"" + entityName + "\">" + tailStr;
		xmlStr += headerStr + " " + genInsertSqlStr(tableName, colNames) + tailStr;
		xmlStr += headerStr + "</insert>" + tailStr;
		
		// updateEntity
		xmlStr += headerStr + "<!-- 更新实体 -->" + tailStr;
		xmlStr += headerStr + "<update id=\"updateEntity\" parameterType=\"" + entityName + "\">" + tailStr;
		xmlStr += headerStr + " " + genUpdateSqlStr(tableName, colNames) + tailStr;
		xmlStr += headerStr + "</update>" + tailStr;
		
		// deleteEntity
		xmlStr += headerStr + "<!-- 删除实体 物理刪除-->" + tailStr;
		xmlStr += headerStr + "<delete id=\"deleteEntity\" parameterType=\"" + entityName + "\">" + tailStr;
		xmlStr += headerStr + " delete from " + tableName + " where id = #{id}" + tailStr;
		xmlStr += headerStr + "</delete>" + tailStr;
		// deleteLogicEntity
		xmlStr += headerStr + "<!-- 删除实体 逻辑刪除-->" + tailStr;
		xmlStr += headerStr + "<update id=\"deleteLogicEntity\" parameterType=\"" + entityName + "\">" + tailStr;
		xmlStr += headerStr + " update  " + tableName + " set last_update_by=#{last_update_by} ,last_update_name=#{last_update_name},last_update_date=#{last_update_date},delete_flag=#{delete_flag} where id = #{id}" + tailStr;
		xmlStr += headerStr + "</update>" + tailStr;
		
		
		// fecthOneEntity
		xmlStr += headerStr + "<!-- 获取单个实体 -->" + tailStr;
		xmlStr += headerStr + "<select id=\"fecthOneEntity\" parameterType=\"" + entityName + "\" resultType=\"" + entityName + "\">" + tailStr;
		xmlStr += headerStr + " select * from " + tableName + " where id = #{id}" + tailStr;
		xmlStr += headerStr + "</select>" + tailStr;
		
		// queryEntities
		xmlStr += headerStr + "<!-- 查询实体集合 -->" + tailStr;
		xmlStr += headerStr + "<select id=\"queryEntities\" parameterType=\"" + entityName + "\" resultType=\"" + entityName + "\">" + tailStr;
		xmlStr += headerStr + " select * from " + tableName +" order by id desc" + tailStr;
		xmlStr += headerStr + "</select>" + tailStr;
		
		// 分页查询
		xmlStr += headerStr + "<!-- 分页查询 -->" + tailStr;
		xmlStr += headerStr + "<select id=\"queryDataGrid\" parameterType=\"" + entityName + "\" resultType=\"" + entityName + "\">" + tailStr;
		xmlStr += headerStr + " select * from " + tableName + " where 1=1 order by id desc limit #{start},#{rows} " + tailStr;
		xmlStr += headerStr + "</select>" + tailStr;

		xmlStr += headerStr + "<select id=\"queryDataGrid_count\" parameterType=\"" + entityName + "\" resultType=\"int\">" + tailStr;
		xmlStr += headerStr + " select count(*) from " + tableName + tailStr;
		xmlStr += headerStr + "</select>" + tailStr;

		xmlStr += "</mapper>";
		FileUtil.writeFile(servicePackage, capName + ".xml", xmlStr);
		return xmlStr;
	}
	
	
	public static String genInsertSqlStr(String tableName, List<String> colNames) {
		String sqlStr = "insert into " + tableName + " (";
		String fields = "";
		String values = "";

		for (String colName : colNames) {
			if ("id".equals(colName)) {
				continue;
			}
			fields += colName + ",";
			values += "#{" + colName + "},";
		}

		fields = fields.substring(0, fields.length() - 1);
		values = values.substring(0, values.length() - 1);
		sqlStr += fields + ") " + tailStr;
		sqlStr += headerStr + " values " + tailStr;
		sqlStr += headerStr + "(" + values + ")";
		return sqlStr;
	}
	
	public static String genUpdateSqlStr(String tableName, List<String> colNames) {
		String sqlStr = "update " + tableName + " set ";

		String fields = "";

		for (String colName : colNames) {
			if ("id".equals(colName) || "create_by".equals(colName) || "create_name".equals(colName) || "create_date".equals(colName)) {
				continue;
			}
			fields += colName + "=#{" + colName + "},";
		}

		fields = fields.substring(0, fields.length() - 1);
		sqlStr += fields + "\r\n";
		sqlStr += headerStr + " where id =#{id}";
		return sqlStr;
	}
	
	public static String sqlType2JavaType(String sqlType) {

		if (sqlType.equalsIgnoreCase("bit")) {
			return "boolean";
		} else if (sqlType.equalsIgnoreCase("tinyint")) {
			return "byte";
		} else if (sqlType.equalsIgnoreCase("smallint")) {
			return "short";
		} else if (sqlType.equalsIgnoreCase("int")) {
			return "int";
		} else if (sqlType.equalsIgnoreCase("bigint")) {
			return "long";
		} else if (sqlType.equalsIgnoreCase("float")) {
			return "float";
		} else if (sqlType.equalsIgnoreCase("decimal") || sqlType.equalsIgnoreCase("numeric") || sqlType.equalsIgnoreCase("real") || sqlType.equalsIgnoreCase("money")
				|| sqlType.equalsIgnoreCase("smallmoney")) {
			return "double";
		} else if (sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char") || sqlType.equalsIgnoreCase("nvarchar") || sqlType.equalsIgnoreCase("nchar")
				|| sqlType.equalsIgnoreCase("text")) {
			return "String";
		} else if (sqlType.equalsIgnoreCase("datetime")) {
			return "Date";
		} else if (sqlType.equalsIgnoreCase("image")) {
			return "Blod";
		}

		return null;
	}
	
	public static void getAllAttrs(StringBuffer sb, List<String> colNames, List<String> colTypes) {

		for (int i = 0; i < colNames.size(); i++) {
			if("id".endsWith(colNames.get(i))){
				continue;
			}
			sb.append("\tprivate " + sqlType2JavaType(colTypes.get(i)) + " " + colNames.get(i) + ";\r\n");
		}
	}
	
	public static String parseEntity(String packageName, String tableName, List<String> colNames, List<String> colTypes, int[] colSizes) {
		String servicePackage = packageName + ".entity";
		StringBuffer sb = new StringBuffer();
		sb.append("package " + servicePackage + ";\r\n");
		sb.append("\r\n");
		sb.append("import cn.uniquetech.entity.base.BaseForm;\r\n");

		String clazz = StrUtil.initCap(tableName);
		clazz = clazz.substring(0, clazz.length() - 1);
		sb.append("@SuppressWarnings(\"serial\")" + "\r\n");
		sb.append("public class " + clazz + entity+ " extends BaseForm" + " {\r\n");
		getAllAttrs(sb, colNames, colTypes);
		sb.append("}\r\n");

		FileUtil.writeFile(servicePackage, clazz + entity +".java", sb.toString());
		return sb.toString();
	}
	
	// service 生成接口
	public static String parseServiceInterface(String packageName, String tableName) {
		String servicePackage = packageName + ".service";
		StringBuffer sb = new StringBuffer();
		sb.append("package " + servicePackage + ";\r\n");
		String clazz = StrUtil.getSimpleClassName(tableName, true);
		sb.append("import cn.uniquetech.service.base.IBaseService;\r\n");
		sb.append("\r\n\r\npublic interface I" + clazz + "Service extends IBaseService" + "{\r\n\r\n");
		sb.append("}\r\n");
		FileUtil.writeFile(servicePackage, "I" + clazz + "Service.java", sb.toString());
		return sb.toString();
	}
	
	// service 生成实现类
	public static String parseServiceClass(String packageName, String tableName) {
		String servicePackage = packageName + ".service.impl";
		StringBuffer sb = new StringBuffer();
		sb.append("package " + servicePackage + ";\r\n\r\n");
		String clazz = StrUtil.getSimpleClassName(tableName, true);
		sb.append("import org.springframework.stereotype.Service;\r\n");
		sb.append("import cn.uniquetech.service.I" + clazz + "Service;");
		sb.append("\r\n\r\n");
		sb.append("@Service");
		sb.append("\r\n");
		sb.append("public class " + clazz + "ServiceImpl extends BaseServiceImpl implements I" + clazz + "Service" + " {\r\n\r\n");
		//sb.append("private I" + clazz + "Service " + clazz.toLowerCase() + "Service;\r\n");
		sb.append("public " + clazz + "ServiceImpl() {\r\n");
		sb.append("\t\tsuper();\r\n");
		sb.append("\t\tthis.mapper = \"" + clazz + "Mapper\";\r\n");
		sb.append("\t}\r\n");
		sb.append("}\r\n");
		FileUtil.writeFile(servicePackage, clazz + "ServiceImpl.java", sb.toString());
		return sb.toString();
	}
	
	// 生成 controller
	public static String parseControllerClass(String packageName, String tableName) {
		String servicePackage = packageName + ".controller";
		String clazz = StrUtil.getSimpleClassName(tableName, true);
		StringBuffer sb = new StringBuffer();
		sb.append("package " + servicePackage + ";\r\n\r\n");
		sb.append("import org.springframework.beans.factory.annotation.Autowired;\r\n");
		sb.append("import org.springframework.stereotype.Controller;\r\n");
		sb.append("import org.springframework.web.bind.annotation.RequestMapping;\r\n");
		sb.append("import cn.uniquetech.controller.base.FormController;\r\n");
		sb.append("import cn.uniquetech.entity." + clazz + entity +";\r\n");
		sb.append("import cn.uniquetech.service.I" + clazz + "Service;\r\n");
		sb.append("\r\n");
		sb.append("@Controller");
		sb.append("\r\n");
		sb.append("@SuppressWarnings(\"unused\")");
		sb.append("\r\n");
		sb.append("@RequestMapping(value = \"/" + clazz.toLowerCase() + "\")");
		sb.append("\r\n");
		sb.append("public class " + clazz + "Controller extends FormController<" + clazz + entity +">" + "{\r\n\r\n");
		sb.append("private I" + clazz + "Service " + clazz.toLowerCase() + "Service;" + "\r\n");
		sb.append("@Autowired\r\n");
		sb.append("public " + clazz + "Controller(I" + clazz + "Service " + clazz.toLowerCase() + "Service) {\r\n");
		sb.append("\t\tsuper();\r\n");
		sb.append("\t\t" + "this." + clazz.toLowerCase() + "Service = " + clazz.toLowerCase() + "Service;" + "\r\n");
		sb.append("\t\t" + "this.baseService = " + clazz.toLowerCase() + "Service;" + "\r\n");
		sb.append("\t\t" + "this.ctrlModel = \"" + clazz.toLowerCase() + "\";" + "\r\n");
		sb.append("\t\t" + "this.ctrlName = \"" + clazz.toLowerCase() + "\";\r\n");
		sb.append("\t\t" + "this.indexUrl = \"" + clazz.toLowerCase()+"/index" + "\";\r\n");
		sb.append("\t}\r\n");
		sb.append("}\r\n");
		FileUtil.writeFile(servicePackage, clazz + "Controller.java", sb.toString());
		return sb.toString();
	}
	
	public static String sc(String packageName, String tableName,List<String> colNames,List<String> colTypes, int[] colSizes){
		String result = "";
		result += paraseSql(packageName,tableName,colNames);
		result += parseEntity(packageName,tableName,colNames,colTypes,colSizes);
		result += parseServiceClass(packageName,tableName);
		result += parseControllerClass(packageName,tableName);
		result += parseServiceInterface(packageName,tableName);
		return result;
	}
	
	
	
	
	
	
}
